/* Animation bar */
$(document).ready(function() {
	$('#progress').removeClass('running');	    
    $('.trigger').click(function() {
        $('#progress').removeClass('running').delay(10).queue(function(next){
            $(this).addClass('running');
            next();
        });
        return false;
    });
});

$(document).ready(function() {
	$('#loading-bar').removeClass('fullwidth');	    
    $('.triggerFull').click(function() {
        $('#content').removeClass('fullwidth').delay(10).queue(function(next){
            $(this).addClass('fullwidth');
            next();
        });
        return false;
    });
});

$(document).ready(function() {
	$('#loadbar').removeClass('ins');	    
		$('.triggerBar').click(function() {
			$('#loadbar').removeClass('ins').delay(10).queue(function(next){
				$(this).addClass('ins');
		        next();
		    });
		    return false;
		});
});

$(document).ready(function(){
	
	var items = $('#stage li'),
		itemsByTags = {};
	
	// Looping though all the li items:
	
	items.each(function(i){
		var elem = $(this),
			tags = elem.data('tags').split(',');
		
		// Adding a data-id attribute. Required by the Quicksand plugin:
		elem.attr('data-id',i);
		
		$.each(tags,function(key,value){
			
			// Removing extra whitespace:
			value = $.trim(value);
			
			if(!(value in itemsByTags)){
				// Create an empty array to hold this item:
				itemsByTags[value] = [];
			}
			
			// Each item is added to one array per tag:
			itemsByTags[value].push(elem);
		});
		
	});

	// Creating the "Everything" option in the menu:
	createList('All',items);

	// Looping though the arrays in itemsByTags:
	$.each(itemsByTags,function(k,v){
		createList(k,v);
	});
	
	$('#filter a').live('click',function(e){
		var link = $(this);
		
		link.addClass('active').siblings().removeClass('active');
		
		// Using the Quicksand plugin to animate the li items.
		// It uses data('list') defined by our createList function:
		
		$('#stage').quicksand(link.data('list').find('li'));
		e.preventDefault();
	});
	
	$('#filter a:first').click();
	
	function createList(text,items){
		
		// This is a helper function that takes the
		// text of a menu button and array of li items
		
		// Creating an empty unordered list:
		var ul = $('<ul>',{'class':'hidden'});
		
		$.each(items,function(){
			// Creating a copy of each li item
			// and adding it to the list:
			
			$(this).clone().appendTo(ul);
		});

		ul.appendTo('#container');

		// Creating a menu item. The unordered list is added
		// as a data parameter (available via .data('list'):
		
		var a = $('<a>',{
			html: text,
			href:'#',
			data: {list:ul}
		}).appendTo('#filter');
	}
});

function showDetail(base_uri, hash) {
    if(hash.length == 0) return;
    console.info(hash);
    
    hash = hash.replace("#","");
    // $('#loading-bar .expand').css('width', '100%');
    
    console.info(hash);
    $('#loading-bar').removeClass('fullwidth').delay(5).queue(function(next){
        $(this).addClass('fullwidth');
        next();
    });
    
        var post_url = base_uri + "/?p=" + hash;
    
    $.get(post_url, function(data) {
        // Hide list
        $("#stage").slideToggle('slow');
        //console.info(data);
        $(data).appendTo(".portfoliocontainer");
        
        // Hide loading bar
        $('#loading-bar').removeClass('fullwidth');
    });
    
    
    // $ret_single_html = $.get
        
    // $(portf_content).slideToggle('slow');
}

function showList() {
    if(!$("#stage").is(":visible")) {
        $(".portfolio-detail").slideToggle('slow');
        $(".portfolio-detail").remove();
        $("#stage").slideToggle('slow');
    }
}